package com.w1.security.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.w1.security.domain.LoginUser;
import com.w1.security.domain.ResponseResult;
import com.w1.security.domain.User;
import com.w1.security.domain.Vo.FindLikePageVo;
import com.w1.security.domain.Vo.PHBListVo;
import com.w1.security.querymodel.InsertUserParm;
import com.w1.security.querymodel.QueryPageInfo;
import com.w1.security.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表的操作
 * @author mingyifan
 * @date 2022-03-07 16:51
 */
@RestController
public class UserController {
    @Autowired
    private IUserService iUserService;
    //增
    @RequestMapping("/insertUser")
    public ResponseResult insertUser(@RequestBody InsertUserParm insertUserParm, HttpServletRequest request){
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
        String s="0123456789";
        String username = insertUserParm.getUsername();
        //邮箱注册
        if (username.contains("@")){
            insertUserParm.setEmail(insertUserParm.getUsername());
            insertUserParm.setUsername(null);
        }
        int c = Integer.valueOf(username.charAt(0));
        //QQ号注册
        if (c>=48&&c<59){
            //数字开头
            insertUserParm.setQq(insertUserParm.getUsername());
            insertUserParm.setUsername(null);
        }
        //我们系统的账号无限制，随便用特殊字符
        //ip相关
        insertUserParm.setIpAddress(remoteAddr);
        insertUserParm.setLoginip(remoteAddr);
            //通过ip获取地理位置
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ip",remoteAddr);
//        map.put("ip","222.92.71.78");
        map.put("json","true");
        String rspStr = "";
        rspStr = HttpUtil.get("http://whois.pconline.com.cn/ipJson.jsp", map);
        if (!StringUtils.hasText(rspStr))
        {
            return new ResponseResult(200,"获取地理信息异常");
        }
        JSONObject obj = JSONObject.parseObject(rspStr);
        String addr = obj.getString("addr");
        insertUserParm.setIpLocation(addr);
        //密码加密
        String pwd = new BCryptPasswordEncoder().encode(insertUserParm.getPassword());
        insertUserParm.setPassword(pwd);
        //insert update user相关
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String loginUsername =loginUser.getUser().getId();
        if (StringUtils.hasText(loginUser.getUser().getQqname())){
            loginUsername=loginUser.getUser().getQqname();
        }
        insertUserParm.setInserUser(loginUsername);
        insertUserParm.setUpdateUser(loginUsername);

        //写
        int insert = iUserService.getBaseMapper().insert(insertUserParm);
        if (insert==1){
            return new ResponseResult(200,"注册成功");
        }
        return new ResponseResult(200,"注册失败",insertUserParm);
    }
    //判断是否重复，每次离开输入账号框，提交一次
    @PostMapping("/isRepeat")
    public ResponseResult isRepeat(@RequestBody String username){
        JSONObject jsonObject = JSON.parseObject(username);
        String userName = jsonObject.getString("username");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userName).or().eq("email",userName).or().eq("qq",userName);
        //拿数据库
        Page<User> userPage1 = iUserService.getBaseMapper().selectPage(new Page<User>(1,10), wrapper);
        List<User> users = userPage1.getRecords();
        if (ObjectUtils.isEmpty(users)){
            return new ResponseResult(200,"无重复，可以使用");
        }
        return new ResponseResult(200,"账号已存在",userName);
    }
}
